const Base = require('../base.js');

module.exports = class extends Base {
  /**
   *
   * @api {GET} /orginfo/{id} 获取当前用户的机构id，与全部机构列表
   * @apiName 获取当前用户的机构id，与全部机构列表
   * @apiGroup userinfo
   * @apiVersion  1.0.0
   *
   * @apiSuccess (200) {Object} org 机构列表
   *
   * @apiSuccess (200) {userOrgId} userOrgId 人员对应的机构id
   *
   * @apiSuccess (200) {allOrg[]} allOrg 全部机构列表
   *
   * @apiSuccess (200) {id} allOrg.id 机构id
   *
   * @apiSuccess (200) {name} allOrg.id 机构名称
   *
   * @apiSuccessExample {type} Success-Response:
   *
   * {
   *  "userOrgId": 8,
   *  "allOrg": [
   *     {
   *        "id": 1,
   *        "name": "蓝色星际"
   *     }
   *   ]
   * }
   *
   *
   */
  async getAction() {
    try {
      const id = this.get('id');
      const org = think.model('org');
      const orgData = await org.field('id, name').select();
      const { getUserOrg } = await think.service('userinfo/user');
      const userOrg = await getUserOrg(id);
      this.body = {
        userOrgId: userOrg.id,
        allOrg: orgData
      };
    } catch (error) {
      this.fail('502');
      think.logger.debug(error);
    }
  }
};
